
package view;

import java.awt.Color;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import javax.swing.ImageIcon;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

import model.core.torneo.Torneo;
import model.core.utente.Giocatore;
import model.core.utente.Studente;
import model.persistence.mysql.DAOManager_MySQL;

/**
 * 
 * @author Giuseppe Fusco (fusgius@gmail.com)
 *
 */
public class PartecipantiSingoliTorneoVistaLabel extends javax.swing.JPanel {

	/**
	 * 
	 */
	private static final long serialVersionUID = 3622117068617857245L;

	private DAOManager_MySQL dao;
	private Torneo torneo;
	private List<JPanel> listPanel;
	private int index = 0;
	private GridLayout grid = new GridLayout(10, 10);

	private ActionListener listener = new ActionListener() {

		@Override
		public void actionPerformed(ActionEvent arg0) {
			// TODO Auto-generated method stub
			Object o = arg0.getSource();
			if(o instanceof GiocatoreToggleButton){
				GiocatoreToggleButton gio = (GiocatoreToggleButton)o;
				if(gio.isStateAdded()){
					if(torneo.removeGiocatore(Giocatore.createFromStudente(gio.getStudente()))){
						gio.setIcon(new ImageIcon("src/view/icon/playerDeselected.png"));
						gio.setForeground(Color.BLACK);
						System.out.println("Remove: "+gio.getStudente().getMatricola());
						gio.setStateAdded(false);
					}
					else
						JOptionPane.showMessageDialog(getParent(), "Hai raggionto il numero minimo di partecipanti.");
				}else{
					if(torneo.addGiocatore(Giocatore.createFromStudente(gio.getStudente()))){
						gio.setIcon(new ImageIcon("src/view/icon/playerSelected.png"));
						gio.setForeground(Color.yellow);
						System.out.println("Add: "+gio.getStudente().getMatricola());
						gio.setStateAdded(true);
					}
					else
						JOptionPane.showMessageDialog(getParent(), "Hai raggionto il numero massimo di partecipanti.");
				}
			}
		}
	};

	public PartecipantiSingoliTorneoVistaLabel(Torneo torneo) {
		initComponents();
		this.torneo = torneo;
		listPanel = new ArrayList<JPanel>();
		try {
			int i = 1;
			dao = DAOManager_MySQL.getInstance();
			JPanel first = new JPanel(grid);
			listPanel.add(first);
			List<Studente> std = dao.getStudenteDao().load("");
			for(Studente s: std){
				GiocatoreToggleButton g = new GiocatoreToggleButton(s);
				g.setIcon(new ImageIcon("src/view/icon/playerDeselected.png"));
				g.addActionListener(listener);
				if(i%100==1 && i!=1){
					JPanel sec = new JPanel(grid);
					listPanel.add(sec);
					sec.add(g);
					sec.repaint();
					sec.revalidate();
					index++;
				}else{
					JPanel p = listPanel.get(index);
					p.add(g);
					p.repaint();
					p.revalidate();
				}
				i++;
			}
			index = 0;
			nroPartecipanti.setText(String.valueOf(i));
			jScrollPane2.setViewportView(listPanel.get(index));
			repaint();
			revalidate();
			addListener();
		} catch (ClassNotFoundException | IOException | SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	private void addListener(){
		next.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent arg0) {
				// TODO Auto-generated method stub
				if(index<listPanel.size()-1)
					jScrollPane2.setViewportView(listPanel.get(++index));
			}
		});

		previous.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent arg0) {
				// TODO Auto-generated method stub
				if(index>0)
					jScrollPane2.setViewportView(listPanel.get(--index));
			}
		});

		first.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent arg0) {
				// TODO Auto-generated method stub
				index = 0;
				jScrollPane2.setViewportView(listPanel.get(index));
			}
		});

		last.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent arg0) {
				// TODO Auto-generated method stub
				index = listPanel.size()-1;
				jScrollPane2.setViewportView(listPanel.get(index)); 
			}
		});

		fatto.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent arg0) {
				// TODO Auto-generated method stub
				System.out.println(torneo.getClassifica().getGiocatori().size());
			}
		});
	}

	private void initComponents() {

		jScrollPane1 = new javax.swing.JScrollPane();
		jPanel1 = new javax.swing.JPanel();
		jScrollPane2 = new javax.swing.JScrollPane();
		annulla = new javax.swing.JButton();
		fatto = new javax.swing.JButton();
		nroPartecipanti = new javax.swing.JTextField();
		javax.swing.JLabel jLabel6 = new javax.swing.JLabel();
		jPanel2 = new javax.swing.JPanel();
		first = new javax.swing.JButton();
		previous = new javax.swing.JButton();
		jLabel1 = new javax.swing.JLabel();
		next = new javax.swing.JButton();
		last = new javax.swing.JButton();

		annulla.setFont(new java.awt.Font("Trebuchet MS", 1, 12)); // NOI18N
		annulla.setText("Annulla");

		fatto.setFont(new java.awt.Font("Trebuchet MS", 1, 12)); // NOI18N
		fatto.setText("Fatto");

		nroPartecipanti.setEditable(false);
		nroPartecipanti.setColumns(4);
		nroPartecipanti.setFont(new java.awt.Font("Trebuchet MS", 1, 12)); // NOI18N

		jLabel6.setFont(new java.awt.Font("Trebuchet MS", 1, 12)); // NOI18N
		jLabel6.setText("Nro partecipanti");

		first.setText("<<");
		jPanel2.add(first);

		previous.setText("<");
		jPanel2.add(previous);

		jLabel1.setText("--");
		jPanel2.add(jLabel1);

		next.setText(">");
		jPanel2.add(next);

		last.setText(">>");
		jPanel2.add(last);

		javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
		jPanel1.setLayout(jPanel1Layout);
		jPanel1Layout.setHorizontalGroup(
				jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
				.addGroup(jPanel1Layout.createSequentialGroup()
						.addContainerGap()
						.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
								.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
										.addGap(0, 260, Short.MAX_VALUE)
										.addComponent(jLabel6, javax.swing.GroupLayout.PREFERRED_SIZE, 95, javax.swing.GroupLayout.PREFERRED_SIZE)
										.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
										.addComponent(nroPartecipanti, javax.swing.GroupLayout.PREFERRED_SIZE, 48, javax.swing.GroupLayout.PREFERRED_SIZE)
										.addGap(18, 18, 18)
										.addComponent(annulla)
										.addGap(0, 0, 0)
										.addComponent(fatto))
										.addComponent(jScrollPane2)
										.addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
										.addContainerGap())
				);
		jPanel1Layout.setVerticalGroup(
				jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
				.addGroup(jPanel1Layout.createSequentialGroup()
						.addContainerGap()
						.addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
						.addGap(1, 1, 1)
						.addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 332, Short.MAX_VALUE)
						.addGap(18, 18, 18)
						.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
								.addComponent(annulla)
								.addComponent(fatto)
								.addComponent(jLabel6)
								.addComponent(nroPartecipanti, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
								.addContainerGap())
				);

		jScrollPane1.setViewportView(jPanel1);

		javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
		this.setLayout(layout);
		layout.setHorizontalGroup(
				layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
				.addComponent(jScrollPane1)
				);
		layout.setVerticalGroup(
				layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
				.addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 420, Short.MAX_VALUE)
				);
	}

	private javax.swing.JButton annulla;
	private javax.swing.JButton fatto;
	private javax.swing.JButton first;
	private javax.swing.JLabel jLabel1;
	private javax.swing.JPanel jPanel1;
	private javax.swing.JPanel jPanel2;
	private javax.swing.JScrollPane jScrollPane1;
	private javax.swing.JScrollPane jScrollPane2;
	private javax.swing.JButton last;
	private javax.swing.JButton next;
	private javax.swing.JTextField nroPartecipanti;
	private javax.swing.JButton previous;

}
